package com.study.android.app.widget;

import android.app.IntentService;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.today.step.lib.Logger;

import java.text.DateFormat;
import java.util.Timer;
import java.util.TimerTask;

public class RegularlySendBroadcastService extends Service {
    private final String TAG = this.getClass().getSimpleName();
    private static final String ACTION_REGULARLY_SEND_BROADCAST = "com.study.widget.REGULARLY_SEND_BROADCAST";
    private final int MSG_UPDATE_WIDGET = 1;

    private Timer timer;
    private TimerTask timerTask;
    private Handler handler;

    @Override
    public void onCreate() {
        Logger.i(TAG, "onCreate");
        super.onCreate();
        handler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case MSG_UPDATE_WIDGET:
                        updateWidget();
                        break;
                    default:
                        break;
                }
            }
        };
    }

    @Override
    public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
        Logger.i(TAG, "onStartCommand");
        onHandleIntent(intent);
        return START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    /**
     * Starts this service to perform action Foo with the given parameters. If
     * the service is already performing a task this action will be queued.
     *
     * @see IntentService
     */
    public static void startRegularlySendBroadcast(Context context) {
        Intent intent = new Intent(context, RegularlySendBroadcastService.class);
        intent.setAction(ACTION_REGULARLY_SEND_BROADCAST);
        context.startService(intent);
    }

    public static void stopRegularlySendBroadcast(Context context) {
        Intent intent = new Intent(context, RegularlySendBroadcastService.class);
        intent.setAction(ACTION_REGULARLY_SEND_BROADCAST);
        context.stopService(intent);
    }

    protected void onHandleIntent(Intent intent) {
        Logger.i(TAG, "onHandleIntent");
        if (intent != null) {
            final String action = intent.getAction();
            Logger.i(TAG, "onHandleIntent,action=" + action);
            if (isRegularlySendBroadcast(action)) {
                handleActionRegularlySendBroadcast();
            } else {
                Logger.i(TAG, "Un");
            }
        }
    }

    private boolean isRegularlySendBroadcast(String action) {
        return TextUtils.equals(ACTION_REGULARLY_SEND_BROADCAST, action);
    }

    private void handleActionRegularlySendBroadcast() {
        Logger.i(TAG, "handleActionRegularlySendBroadcast");
        if (null == timer) {
            timer = new Timer();
        }
        if (null == timerTask) {
            timerTask = new TimerTask() {
                @Override
                public void run() {
                    handler.sendEmptyMessage(MSG_UPDATE_WIDGET);
                }
            };
        }
        timer.schedule(timerTask, 0, 10 * 1000);
    }

    private void updateWidget() {
        DateFormat dateFormat = DateFormat.getDateTimeInstance();
        Logger.i(TAG, "updateWidget,time=" + dateFormat.format(System.currentTimeMillis()));
        Intent intent = new Intent(TestAppWidgetProvider.INTENT_ACTION_APPWIDGET_UPDATE);
        intent.setClass(this, TestAppWidgetProvider.class);
        sendBroadcast(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Logger.i(TAG, "onDestroy");
        timer.cancel();
    }
}
